package org.sunshine.miracle.ui;

import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class StringBundle {

    private static ResourceBundle resourceBundle;

    private static ResourceBundle getResourseBundle() {
        if (resourceBundle == null) {
            resourceBundle = ResourceBundle.getBundle("org.sunshine.miracle.ui.messages");
        }
        return resourceBundle;
    }

    static String getResourceString(String key) {
        try {
            return getResourseBundle().getString(key);
        }
        catch (MissingResourceException e) {
            return key;
        } catch (NullPointerException e) {
            return "!" + key + "!";
        }
    }

    /**
     * Returns a string from the resource bundle and binds it
     * with the given arguments. If the key is not found,
     * return the key.
     */
    static String getResourceString(String key, Object[] args) {
        try {
            return MessageFormat.format(getResourceString(key), args);
        } catch (MissingResourceException e) {
            return key;
        } catch (NullPointerException e) {
            return "!" + key + "!";
        }
    }


}
